<template>
	<view>
		<view :style="{height: titleNavHeight + statusBarHeight + 5 + 'px'}" style="position: fixed;top: 0;left: 0;right: 0;z-index: 9999;background-color: #ffffff;
			border-bottom: 1px solid #ebedf0;">
			<view :style="{height: titleNavHeight + 'px', top: statusBarHeight + 'px', width: '100%'}" class="nav-bar">
				<text class="text">我的</text>
			</view>
		</view>
		<view :style="{height: titleNavHeight + statusBarHeight + 5 + 'px'}">33</view>
		<view style="background: #f1f1f1; position: absolute;top: 0px;bottom: 0px;right: 0;left: 0;padding: 2px 0;">
			<view class="content" style="background: #f1f1f1;">
				<!-- <image class="textIcon" src="../../static/my/myTextIcon2x.png" mode="widthFix" /> -->
				<view class="header-section">
					<view class="flex-row-between">
						<view class="flex align-center">
							<image class="image-avater" :src="imageSrc" @click="handleToAvatar" mode="widthFix" />
							<view class="user-info">
								<view class="u_title">
									id：{{userName}}
								</view>
							</view>
						</view>
						<!-- <view @click="handleToInfo">
							<text>账号信息</text>
							<u-icon name="arrow-right"></u-icon>
						</view> -->
					</view>
				</view>

				<view class="cell">
					<u-cell-group :border="true" style="width: 100%;">
						<u-cell-item :arrow="true" v-if="isFalg==1" title="学校资料" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(1)">
							<u-icon slot="icon" size="32" name="account"></u-icon>
						</u-cell-item>
						<u-cell-item :arrow="true" v-if="isFalg==2" title="个人资料" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(2)">
							<u-icon slot="icon" size="32" name="account"></u-icon>
						</u-cell-item>
						<u-cell-item :arrow="true" v-if="isFalg==3" title="老师资料" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(3)">
							<u-icon slot="icon" size="32" name="account"></u-icon>
						</u-cell-item>
						<u-cell-item :arrow="true" title="常见问题" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(4)">
							<u-icon slot="icon" size="32" name="info-circle"></u-icon>
						</u-cell-item>
						<u-cell-item :arrow="true" title="应用设置" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(5)">
							<u-icon slot="icon" v-if="isShow" size="32" name="setting" class="dot"></u-icon>
							<u-icon slot="icon" v-else size="32" name="setting"></u-icon>
						</u-cell-item>
						<u-cell-item :arrow="true" title="AI助手" :border-bottom="false" :center="true"
							:title-style="titleStyle" @click="goTaskList(6)">
							<u-icon slot="icon" size="32" name="chat"></u-icon>
						</u-cell-item>
					</u-cell-group>
				</view>

				<u-button style="margin: 10vh 0; background-color: #027bd5;color: #ffff;" class="btn" @click="onLogOut()">退出登录</u-button>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		SystemController
	} from '@/api';

	import {
		getBoundingClientRect,
		getToken
	} from "@/utils";

	import filePersistentIO from "@/utils/filePersistentIO";

	import projectConfig from '@/core/config'

	export default {
		data() {
			const {
				windowWidth,
				statusBarHeight
			} = uni.getSystemInfoSync(windowWidth, statusBarHeight)

			const rect = getBoundingClientRect(windowWidth, statusBarHeight)

			return {
				imageSrc: "",
				userName: uni.getStorageSync('userInfo').showName,
				windowWidth: windowWidth,
				statusBarHeight: statusBarHeight,
				titleNavHeight: (rect.top - statusBarHeight) * 2 + rect.height,
				emptyWidth: rect.left - 10,
				headerHeight: 0,
				titleStyle: {
					'color': '#333333',
					'font-size': '32rpx',
					'margin-left': '20rpx'
				},
				isFalg: 0,
				isShow:false
			}
		},
		mounted() {
			switch (uni.getStorageSync('userInfo').deptName) {
				case '学校基座':
					this.isFalg = 1
					break;
				case '学员':
					this.isFalg = 2
					break;
				case '老师':
					this.isFalg = 3
					break;
				default:
					this.isFalg = 4
					break
			}
			var headImageUrlObj = uni.getStorageSync('userInfo').headImageUrl
			if (headImageUrlObj) {
				var resObj = JSON.parse(headImageUrlObj)[0];
				this.imageSrc = this.getUploadFileUrl(resObj, resObj.filename);
			}
			this.checkForUpdate()
		},
		onShow() {

			var headImageUrlObj2 = uni.getStorageSync('userInfo').headImageUrl

			if (headImageUrlObj2) {
				var resObj2 = JSON.parse(headImageUrlObj2)[0];
				this.imageSrc = this.getUploadFileUrl(resObj2, resObj2.filename);

			}


		},
		methods: {
			async checkForUpdate() {
				let obj={
					apkVersionDtoFilter:{
						status:1
					}
					
				}
				let res = await SystemController.getApkVersionList(this,obj);
				
				let Response = res.dataList[0]
			
				//获取当前安装包版本号
				const currentVersionCode = await this.getCurrentVersionCodeWay();
				// console.log("当前版本号:", currentVersionCode);
				// console.log("最新版本号:", Response);
				// 对比版本号
				if (Response.latestPersionCode > currentVersionCode) {
					// 显示更新对话框
					// appDialog.show(Response.path, Response.changelog);
					this.isShow = true
				}
			},
			getCurrentVersionCodeWay() {
				return new Promise((resolve) => {
					//获取当前安装包版本号
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						resolve(parseInt(wgtinfo.versionCode));
					});
				});
			},
			async getCurrentVersionCode2() {
				this.currentVersion = await this.getCurrentVersionCode();

			},
			getCurrentVersionCode() {
				return new Promise((resolve) => {
					//获取当前安装包版本号
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						resolve((wgtinfo.version));
					});
				});
			},
			getUploadFileUrl(item, params) {

				if (item == null || item.downloadUri == null) {
					return null;
				} else {

					const query = {};
					query.Authorization = getToken();
					query.filename = params

					return this.buildGetUrl(item.downloadUri, query);
				}
			},

			buildGetUrl(actionName, params) {
				const queryString = this.objectToQueryString(params);
				if (actionName != null && actionName !== '') {
					if (actionName.substring(0, 1) === '/') actionName = actionName.substring(1);


					return (
						projectConfig.baseUrl + actionName + (queryString == null ? '' : '?' + queryString)
					);
				}
			},

			objectToQueryString(params) {
				if (params == null) {
					return null;
				} else {
					return Object.keys(params)
						.map(key => {
							if (params[key] !== undefined) {
								return `${key}=${params[key]}`;
							} else {
								return undefined;
							}
						})
						.filter(item => item != null)
						.join('&');
				}
			},

			handleToInfo() {
				uni.navigateTo({
					url: '/pages/my/info'
				})
			},
			handleToAvatar() {
				uni.navigateTo({
					url: '/pages/my/avatar/avatar?imageSrc=' + encodeURIComponent(this.imageSrc)
				})
			},
			goTaskList(index) {
				switch (index) {
					case 1:
						uni.navigateTo({
							url: '/pages/my/schoolInfo'
						})
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/my/infoEdit'
						})
						break;
					case 3:
						uni.navigateTo({
							url: '/pages/my/teacherInfo'
						})
						break;
					case 4:
						uni.navigateTo({
							url: '/pages/my/help'
						})
						break;
					case 5:
						uni.navigateTo({
							url: '/pages/my/setting'
						})
						break;
					case 6:
						uni.navigateTo({
							url: '/pages/my/AIquestion'
						})
						break;
				}

			},
			onLogOut() {
				SystemController.logout(this).then(res => {
					uni.removeStorage({
						key: 'token',
						success(res) {
							try {
								filePersistentIO.deletes();
							} catch (error) {
								// 浏览器会失效
							}
							uni.reLaunch({
								url: '/pages/login/index'
							});
						}
					})
				});
			}
		}
	}
</script>
<style scoped lang="scss">
	.cell {
		width: 100%;
		background-color: #ffffff;
		border-radius: 16rpx;
		margin: 0;
		display: flex;
		align-items: center;
	}

	.dot::after {
		content: '';
		position: absolute;
		// top: -5px;
		/* 根据图标大小调整 */
		right: 40px;
		/* 根据图标大小调整 */
		width: 10px;
		/* 红点大小 */
		height: 10px;
		/* 红点大小 */
		background-color: red;
		border-radius: 50%;
		/* 圆形红点 */
	}


	.user-info {
		margin-left: 15px;
		display: flex;
		align-items: center;

		.u_title {
			font-size: 18px;
			line-height: 30px;
		}
	}

	.header-section {
		padding: 35px 15px 38px 15px;
		background-color: #027bd5;
		color: white;
		margin: 54px 0 10rpx 0;
		width: 100vw;
	}

	.image-avater {
		border: 2px solid #eaeaea;
		width: 64px;
		height: 64px !important;
		font-size: 2.5em;
		border-radius: 50%;
	}

	.nav-bar {
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 20rpx;

		.text {
			max-width: 60%;
			margin: 0 auto;
			color: #323233;
			font-weight: 500;
			font-size: 30rpx;
		}

		.nav-right {
			display: flex;

			.mobile-font {
				color: #707070;
				font-size: 36rpx;

				&+.mobile-font {
					margin-left: 12rpx;
				}
			}
		}
	}

	.navbar-btm {
		border-bottom: 1px solid #E8E8E8;
	}

	.content {
		width: 100%;
		height: auto;
		display: flex;
		flex-direction: column;
		align-items: center;
		background: white;
	}

	.content .image {
		width: 100%;
		height: auto;
	}

	.content .textIcon {
		width: 70%;
		height: auto;
		margin: 0px 0 4vh 0;
	}

	.content p {
		width: 100%;
		color: #666666;
		font-size: 28rpx;
		margin: 10rpx 0 68rpx 0;
		display: flex;
		flex-direction: row;
		align-items: flex-end;
		justify-content: center;
	}

	.content p span {
		font-size: 32rpx;
		color: $u-type-primary;
	}

	.btn {
		width: 240rpx;
		height: 80rpx !important;
		border-radius: 8rpx !important;
	}
</style>